//引入组件库
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var userUrl = require("url/user");
var httpSilence = require("helper/httpSilence");
var openHelper = require("helper/open");
var userUtil = require("helper/user");
var guideHelper = require("helper/guide");
var friendHashData = mm("do_HashData");
var indexListview = ui("indexListView");

var friendCache = sm("do_DataCache");
var chchekey4friendMap = "cache_qq_user_friend_map_data";
var chchekey4firstChars = "cache_qq_user_friend_first_chars";

//andriod返回键
page.on("back", function() {
	app.closePage();
});

//渲染页面头部begin
var layoutHead = ui("layoutHead");
var addheader = layoutHead.add("header", "source://view/reuse/header.ui", 0, 0);
ui(addheader+".labelTitle").text = "通讯录";
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
  app.closePage();
});
//右上角--图标【菜单】
var headerRight = ui(addheader + ".headerRight");
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;
//渲染页面头部end

//右上角menu--begin
var menus= [];
menus=[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName":"添加好友","type":"addFriend"}];
var popmenu = ui("$").add("menupop", "source://view/reuse/menu/menul.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});

popmenuf.on("popmenuItem", function(data) {
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if(actionType=="addFriend"){
		popmenuf.fire("popmenuClose");
		openHelper.open("source://view/user/friend/friendAddList.ui");//添加好友
	}
});
//右上角menu--end

page.on("loaded", function(data) {
	binddata();
})

function loadCacheData() {
	if(friendCache.hasData(chchekey4firstChars) && 
			friendCache.hasData(chchekey4friendMap)){
		var firstCharsInCache = friendCache.loadData({key:chchekey4firstChars});
		var friendMapInCache = friendCache.loadData({key:chchekey4friendMap});
		//deviceone.print(JSON.stringify(firstCharsInCache),"firstCharsInCache");
		//deviceone.print(JSON.stringify(friendMapInCache),"friendMapInCache");
		showFriendData(firstCharsInCache, friendMapInCache);//显示缓存中的数据
	}
}

function binddata() {
	if(userUtil.loginToken()==""){
		nf.toast("您还没有登陆!")
		return;
	}
	loadCacheData();
	httpSilence.post(userUrl.url.friend.myFriendMap, {}, function(data) {
		var firstChars = data.friendMapKeys;
		var friendMap = data.friendMap;
		showFriendData(firstChars, friendMap);
		friendCache.saveData({key: chchekey4firstChars, value: firstChars});//缓存数据到本地
		friendCache.saveData({key: chchekey4friendMap, value: friendMap});//缓存数据到本地
	});
}

function showFriendData(firstChars,friendMap) {
	friendHashData.removeAll();	
	for (var i = 0; i < firstChars.length; i++) {
		var firstChar = firstChars[i];
		var friendGroupList = friendMap[firstChar];//同一首字母的好友列表
		var friendTemplate = [];
		friendTemplate.push({
			"template" : 0,
			"firstChar" : firstChar
		});
		for (var j = 0; j < friendGroupList.length; j++) {
			var oneFriend = friendGroupList[j];
			friendTemplate.push({
				"template" : 1,
				"friendId" :oneFriend.friendId,	
				"fullFriendLogo" :oneFriend.fullFriendLogo,
				"friendName" :oneFriend.friendName, 
				"remark" : oneFriend.remark, 
				"sexIcon" :oneFriend.sexIcon,
				"sexColor" : oneFriend.sexColor,
				"interests" :oneFriend.interests,
				"interestList" :oneFriend.interestList,
				"neighbor" :oneFriend.neighbor,
				"ourCircleDesc" :oneFriend.ourCircleDesc
			});
		}
		if (firstChar == "A") {
			friendHashData.addData({
				"A" : friendTemplate
			});
		} else if (firstChar == "B") {
			friendHashData.addData({
				"B" : friendTemplate
			});
		} else if (firstChar == "C") {
			friendHashData.addData({
				"C" : friendTemplate
			});
		} else if (firstChar == "D") {
			friendHashData.addData({
				"D" : friendTemplate
			});
		} else if (firstChar == "E") {
			friendHashData.addData({
				"E" : friendTemplate
			});
		} else if (firstChar == "F") {
			friendHashData.addData({
				"F" : friendTemplate
			});
		} else if (firstChar == "G") {
			friendHashData.addData({
				"G" : friendTemplate
			});
		} else if (firstChar == "H") {
			friendHashData.addData({
				"H" : friendTemplate
			});
		} else if (firstChar == "I") {
			friendHashData.addData({
				"I" : friendTemplate
			});
		} else if (firstChar == "J") {
			friendHashData.addData({
				"J" : friendTemplate
			});
		} else if (firstChar == "K") {
			friendHashData.addData({
				"K" : friendTemplate
			});
		} else if (firstChar == "L") {
			friendHashData.addData({
				"L" : friendTemplate
			});
		} else if (firstChar == "M") {
			friendHashData.addData({
				"M" : friendTemplate
			});
		} else if (firstChar == "N") {
			friendHashData.addData({
				"N" : friendTemplate
			});
		} else if (firstChar == "O") {
			friendHashData.addData({
				"O" : friendTemplate
			});
		} else if (firstChar == "P") {
			friendHashData.addData({
				"P" : friendTemplate
			});
		} else if (firstChar == "Q") {
			friendHashData.addData({
				"Q" : friendTemplate
			});
		} else if (firstChar == "R") {
			friendHashData.addData({
				"R" : friendTemplate
			});
		} else if (firstChar == "S") {
			friendHashData.addData({
				"S" : friendTemplate
			});
		} else if (firstChar == "T") {
			friendHashData.addData({
				"T" : friendTemplate
			});
		} else if (firstChar == "U") {
			friendHashData.addData({
				"U" : friendTemplate
			});
		} else if (firstChar == "V") {
			friendHashData.addData({
				"V" : friendTemplate
			});
		} else if (firstChar == "W") {
			friendHashData.addData({
				"W" : friendTemplate
			});
		} else if (firstChar == "X") {
			friendHashData.addData({
				"X" : friendTemplate
			});
		} else if (firstChar == "Y") {
			friendHashData.addData({
				"Y" : friendTemplate
			});
		} else if (firstChar == "Z") {
			friendHashData.addData({
				"Z" : friendTemplate
			});
		}else{
			friendHashData.addData({
				"#" : friendTemplate
			});
		}
	}//for
	indexListview.bindItems(friendHashData, firstChars);
	indexListview.refreshItems();
}

//退出成功后刷新列表
app.on("fireUserLogout",function(data){
	friendHashData.removeAll();
	var friendTemplate = [];
	friendTemplate.push({
		"template" : 0,
		"firstChar" : "#"
	});
	friendHashData.addData({"#" : friendTemplate});
	firstChars = ['#'];
	indexListview.bindItems(friendHashData, firstChars);
	indexListview.refreshItems();
})

app.on("fireUserUpdate",function(data){
	binddata();
});

//群聊
ui("groupChat").on("touch","", 3000,function(){
	//TODO :打开群聊通讯录
	openHelper.open("source://view/circle/gchat/myGroupChat/main.ui", {});
})

//新的好友
ui("newFriend").on("touch","", 3000,function(){
	//TODO :打开好友申请列表
	openHelper.open("source://view/user/friend/newFriends/newFriends.ui", {});
})

indexListview.on("touch","", 3000,function(data, e) {
	var cell = friendHashData.getOne(data.groupID);
    var oneFriend = cell[data.index];
    if(oneFriend){
    	var userId = oneFriend.friendId;
        if(userId){
        	openHelper.openPersonal(userId);
        }
    }
})